/**
 *  research.sbookmarking is the package name for the social bookmarking 
 *  project done by Dr S. Chu
 */
package research.sbookmarking;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import research.sbookmarking.io.BookmarkCommand;
import research.sbookmarking.io.InputController;
import research.sbookmarking.io.OutputController;

/**
 *  This is the main controller for the application. This application also 
 *  uses packages org.connotea, org.afox.formatting, org.afox.parsers.xml,
 *  org.afox.util, org.afox.util.assertImpl, org.afox.util.stringtype,
 *  org.afox.util.validators, org.afox.util.validators.string, and 
 *  org.apache.xerces.parsers.SAXParser
 *  <p>
 *  You can redistribute it and/or modify it under the terms of the GNU
 *  General Public License as published by the Free Software Foundation;
 *  either version 2 of the License, or (at your option) any later version.
 *  <p>
 *  @author <A HREF="mailto:aatleung@gmail.com">Albert Leung</a>
 *  @version 1.0
 *
 */

public class AppController {

	private static AppController anInstance=null;
	private Logger logger = Logger.getLogger("MyLog");
    FileHandler fh; 
	
    public static String newline = System.getProperty("line.separator");
    
	private AppController (){
		try {
			// This block configure the logger with handler and formatter
			fh = new FileHandler("error.log", 1048576, 10, true);
			logger.addHandler(fh);
			logger.setLevel(Level.ALL);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
		} 
		catch (SecurityException e) {
			e.printStackTrace();
		} 
		catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}
	
	public static AppController getInstance(){
		if (anInstance==null) {
			anInstance = new AppController();
		}
		
		return anInstance;
	}
/*	
	@SuppressWarnings("unchecked")
	public void copy(String command, HashMap data, List keys, List values){
		List param = new ArrayList();
		
		for (int i=0; i<keys.size();i++){
			Object obj = data.get(keys.get(i));
			if (obj!=null){
				List<Class<?>> classes = Arrays.asList(obj.getClass().getInterfaces());
				if (StringUtils.contains("java.util.List",classes.iterator())) {
					param.add(obj);
					param.add(values.get(i));
					RMethod.invoke(this, command, param);
					param.clear();
				}
			}
			else {
				obj = values.get(i);
			}
			data.put(keys.get(i), obj);
		}
	}
*/
	
	@SuppressWarnings("unchecked")
	public void copy(List results, List items){
		copy(results,items.iterator());
	}
	
	@SuppressWarnings("unchecked")
	public void copy(List results, Iterator list) {
		while (list.hasNext()){
			copy (results,list.next());
		}
	}
	
	@SuppressWarnings("unchecked")
	public void copy(List results, Object item){
		results.add(item);
	}
	
	@SuppressWarnings("unchecked")
	public void copyUnique(List results, List list) {
		copyUnique(results, list.iterator());
	}
	
	@SuppressWarnings("unchecked")
	public void copyUnique(List results, Iterator list) {
		while (list.hasNext()){
			copyUnique(results,list.next());
		}
	}
	
	@SuppressWarnings("unchecked")
	public void copyUnique(List results, Object item) {
		if (results.contains(item)==false) {
			copy(results,item);
		}
	}
	
	public BookmarkCommand createBookmarkComand (Iterator<String> urls){
		ArrayList<String> queryList = new ArrayList<String>();
		
		while (urls.hasNext()){
			queryList.add(urls.next());
		}
		
		return new BookmarkCommand(null,"URL", queryList,new Integer(10),new Integer(0));
	}
	/**
	 *   This method will be call when the program is executed.
	 *   <p>
	 *   @param args the command-line arguments that is passed to this
	 *   			 program.
	 *   
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		if (args.length<1){
			InputController.getInstance().getQuery("setting.xml");
		}
		else {
			InputController.getInstance().getQuery(args[0]);
		}
		//List<DataContainer> dataContainers = InputController.getInstance().getQuery("setting.xml");
		//ProcessController.getInstance().processData(dataContainers);
		//OutputController.getInstance().saveObjectData("random.raw.gz", dataContainers, true);
		//List queryList = InputController.getInstance().readQueryList("urlList.txt");
		//BookmarkCommand command = AppController.getInstance().createBookmarkComand(queryList.iterator());
		//command.execute();
		//OutputController.getInstance().createXmlQuery("url.xml", "URL", "url", queryList.iterator());

		//ArrayList param = new ArrayList();
		//param.add(command.getResult());
		/*
		OutputController.getInstance().saveData("dates.rtf.gz","From 2008-Jun-01 to 2008-Jun-15",
				(DataContainer)dataContainers.get(0));
		*/
		
	}
	
	public Logger getLogger() {
		return logger;
	}
}
